/**
* 左边菜单
*/
<template>
  <el-menu :collapse="collapsed" collapse-transition router :default-active="activeMenuItem" unique-opened
           class="el-menu-vertical-demo" background-color="#334157" text-color="#fff" active-text-color="#ffd04b" :base-path="'/'">
    <div class="logobox">
      <img class="logoimg" src="/static/zuo_shang_logo.png"  alt="">
    </div>
    <template v-for="menu in leftPression">
      <template v-if="menu.children.length > 0">
        <el-submenu :index="menu.id">
          <template slot="title">
            <span @click="handleSubMenuClick(menu)">{{ menu.permissionName }}</span>
          </template>
          <el-menu-item-group>
            <template v-for="menuchildren in menu.children">
              <template v-if="menuchildren.children && menuchildren.children.length > 0"> <!-- 添加判断是否有第三级菜单 -->
                <el-submenu :index="menuchildren.id">
                  <template slot="title">
                    <span @click="handleSubMenuClick(menuchildren)">{{ menuchildren.permissionName }}</span>
                  </template>
                  <el-menu-item-group>
                    <template v-for="thirdMenu in menuchildren.children">
                      <el-menu-item :index="thirdMenu.id" @click="handleMenuItemClick(thirdMenu)">
                        <span>{{ thirdMenu.permissionName }}</span>
                      </el-menu-item>
                    </template>
                  </el-menu-item-group>
                </el-submenu>
              </template>
              <template v-else>
                <el-menu-item :index="menuchildren.id" @click="handleMenuItemClick(menuchildren)">
                  <span>{{ menuchildren.permissionName }}</span>
                </el-menu-item>
              </template>
            </template>
          </el-menu-item-group>
        </el-submenu>
      </template>
      <template v-else>
        <el-menu-item :index="menu.id" @click="handleMenuItemClick(menu)">
          <span>{{ menu.permissionName }}</span>
        </el-menu-item>
      </template>
    </template>
  </el-menu>
</template>
<script>
import { menu } from '../api/userMG'
export default {
  name: 'leftnav',
  data() {
    return {
      activeMenuItem: null, // 初始化为 null
      collapsed: false,
      allmenu: [],
      leftPression: []
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
    this.getLeftPression()
    this.allmenu = res.data
    // 监听
    this.$root.Bus.$on('toggle', value => {
      this.collapsed = !value
    })
  },
  methods: {
    //获取左侧菜单数据
    getLeftPression() {
      let leftdata = JSON.parse(sessionStorage.getItem('userdata'))
      this.leftPression = leftdata.data
      console.log(this.leftPression, 'leftPression');
    },

    handleSubMenuClick(menu) {
      // 处理点击子菜单的事件
      this.activeMenuItem = menu.id;

      sessionStorage.setItem('permissionName',menu.permissionName)
      sessionStorage.setItem('position',menu.displayPosition)
      this.$router.push({
        path: '/' + menu.resourceAddress,
        query: { permissionName: menu.permissionName, position: menu.displayPosition }
      });
    },
    handleMenuItemClick(menu) {
      // 处理点击子菜单的事件
      this.activeMenuItem = menu.id;

      sessionStorage.setItem('permissionName',menu.permissionName)
      sessionStorage.setItem('position',menu.displayPosition)
      this.$router.push({
        path: '/' + menu.resourceAddress,
        query: { permissionName: menu.permissionName, position: menu.displayPosition }
      });
    }
  }
}
</script>
<style>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 240px;
  min-height: 400px;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
  text-align: left;
}

.el-menu-item-group__title {
  padding: 0px;
}

.el-menu-bg {
  background-color: #1f2d3d !important;
}

.el-menu {
  border: none;
}

.logobox {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  height: 40px;
  line-height: 40px;
  color: #9d9d9d;
  font-size: 20px;
  text-align: center;
  padding: 20px 0px;
}

.logoimg {
  height: 60px;
}
</style>
